package cn.itcast.test;

import cn.itcast.dao.UserDao;
import cn.itcast.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class MyTest01 {
    @Test
    public void findById() throws IOException {
        //读取配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //获取操作对象
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(is).openSession();
        //获取
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        //使用
        User user = userDao.findById(2);
        //释放资源
        sqlSession.close();
        //输出
        System.out.println(user);
    }

    @Test
    public void findByInfo() throws IOException {
        //读取配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //获取操作对象
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(is).openSession();
        //获取
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        //使用
        User u = new User();
        u.setPassword("123456");
        List<User> users = userDao.findByInfo(u);
        //释放资源
        sqlSession.close();
        //输出
        System.out.println(users);
    }

    @Test
    public void findByIds() throws IOException {
        //读取配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //获取操作对象
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(is).openSession();
        //获取
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        //使用
        ArrayList<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(3);
        List<User> users = userDao.findByIds(list);
        //释放资源
        sqlSession.close();
        //输出
        System.out.println(users);
    }
}
